package com.lenovo.manufacture.module;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.widget.TextView;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.annotation.Layout;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import butterknife.BindView;

public class Module4 extends BaseActivity {

    @BindView(R.id.tv_00)
    TextView tv00;
    @BindView(R.id.tv_01)
    TextView tv01;
    @BindView(R.id.tv_03)
    TextView tv03;
    @BindView(R.id.tv_02)
    TextView tv02;
    @BindView(R.id.tv_10)
    TextView tv10;
    @BindView(R.id.tv_11)
    TextView tv11;
    @BindView(R.id.tv_12)
    TextView tv12;
    @BindView(R.id.tv_13)
    TextView tv13;
    @BindView(R.id.tv_20)
    TextView tv20;
    @BindView(R.id.tv_22)
    TextView tv22;
    @BindView(R.id.tv_23)
    TextView tv23;
    @BindView(R.id.tv_21)
    TextView tv21;
    @BindView(R.id.tv_30)
    TextView tv30;
    @BindView(R.id.tv_32)
    TextView tv32;
    @BindView(R.id.tv_33)
    TextView tv33;
    @BindView(R.id.tv_31)
    TextView tv31;
    private List<Map<String, String>> personList;

    @Layout(value = R.layout.activity_module4,title = "分配岗位")
    protected void init() {
        initView();
        async(apiService.getAllPerson(), data -> personList = data.getData());
        initData();
    }

    private void initData() {
        for (int i = 0; i < 4; i++) {
            //通过位置查询生产线
            int finalI = i;
            async(apiService.userLineSearch(param.add("pos",i).build()), data -> {
                String lineId = data.getData().get(0).get("lineId");
                //通过生产线查询岗位
                async(apiService.userPositionSearch(param.add("userLineId",lineId).build()), data1 -> {
                    for (Map<String, String> position : data1.getData()) {
                        if (!position.get("userPersonId").equals("0")) {
                            addPerson(finalI, position.get("userPersonId"), Integer.valueOf(position.get("positionId")));
                        }
                    }
                });
            });
        }
    }

    private void initView() {
        tv00.setOnClickListener(view -> addPosition(0, 0));
        tv01.setOnClickListener(view -> addPosition(0, 1));
        tv03.setOnClickListener(view -> addPosition(0, 3));
        tv02.setOnClickListener(view -> addPosition(0, 2));
        tv10.setOnClickListener(view -> addPosition(1, 0));
        tv11.setOnClickListener(view -> addPosition(1, 1));
        tv12.setOnClickListener(view -> addPosition(1, 2));
        tv13.setOnClickListener(view -> addPosition(1, 3));
        tv20.setOnClickListener(view -> addPosition(2, 0));
        tv22.setOnClickListener(view -> addPosition(2, 2));
        tv23.setOnClickListener(view -> addPosition(2, 3));
        tv21.setOnClickListener(view -> addPosition(2, 1));
        tv30.setOnClickListener(view -> addPosition(3, 0));
        tv32.setOnClickListener(view -> addPosition(3, 2));
        tv33.setOnClickListener(view -> addPosition(3, 3));
        tv31.setOnClickListener(view -> addPosition(3, 1));
    }

    private void addPosition(int pos, int type) {
        json.put("pos", pos);
        async(apiService.userLineSearch(json), data -> {
            String lineId = data.getData().get(0).get("lineId");
            json.put("userLineId", lineId);
            json.put("type", type);
            async(apiService.addUserPosition(json), data1 -> {
                //刷新数据
                initData();
            });
        });
    }

    @SuppressLint("DefaultLocale")
    private void addPerson(int pos, String userId, int position) {
        Map<String, String> map = personList.stream().filter(person1 -> person1.get("id").equals(userId)).collect(Collectors.toList()).get(0);
        TextView textView = getTextView(pos, position);
        textView.setText(String.format("姓名：%s\n体力：%s", map.get("personName"), map.get("power")));
        textView.setBackgroundColor(Color.parseColor("#82aaff"));
        textView.setOnClickListener(view -> showMsg("岗位上已存在人员"));
    }

    private TextView getTextView(int pos, int type) {
        switch (pos) {
            case 0:
                switch (type) {
                    case 0:
                        return tv00;
                    case 1:
                        return tv01;
                    case 2:
                        return tv02;
                    case 3:
                        return tv03;
                }
                break;
            case 1:
                switch (type) {
                    case 0:
                        return tv10;
                    case 1:
                        return tv11;
                    case 2:
                        return tv12;
                    case 3:
                        return tv13;
                }
                break;
            case 2:
                switch (type) {
                    case 0:
                        return tv20;
                    case 1:
                        return tv21;
                    case 2:
                        return tv22;
                    case 3:
                        return tv23;
                }
                break;
            case 3:
                switch (type) {
                    case 0:
                        return tv30;
                    case 1:
                        return tv31;
                    case 2:
                        return tv32;
                    case 3:
                        return tv33;
                }
                break;
        }
        return null;
    }
}
